﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using works6.DAL;

namespace works6
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ObservableCollection<School> schools;
        private ObservableCollection<Class> classes;
        private ObservableCollection<Student> students;
        private ObservableCollection<Log> logs;

        public MainWindow()
        {
            InitializeComponent();
            schools = new ObservableCollection<School>(SchoolDAL.GetAllSchools());
            classes = new ObservableCollection<Class>(ClassDAL.GetAllClasses());
            students = new ObservableCollection<Student>(StudentDAL.GetAllStudents());
            logs = new ObservableCollection<Log>(LogDAL.GetAllLogs());
            DataContext = this;
        }

        public ObservableCollection<School> Schools
        {
            get { return schools; }
            set { schools = value; }
        }

        public ObservableCollection<Class> Classes
        {
            get { return classes; }
            set { classes = value; }
        }

        public ObservableCollection<Student> Students
        {
            get { return students; }
            set { students = value; }
        }

        public ObservableCollection<Log> Logs
        {
            get { return logs; }
            set { logs = value; }
        }

        private void AddSchool_Click(object sender, RoutedEventArgs e)
        {
            AddSchoolWindow addSchoolWindow = new AddSchoolWindow();
            addSchoolWindow.ShowDialog();
            if (addSchoolWindow.DialogResult == true)
            {
                SchoolDAL.AddSchool(addSchoolWindow.School);
                schools.Add(addSchoolWindow.School);
                LogDAL.AddLog(new Log() { LogTime = DateTime.Now.ToString(), LogUser = "管理员", LogContent = "添加学校：" + addSchoolWindow.School.SchoolName });
            }
        }

        private void EditSchool_Click(object sender, RoutedEventArgs e)
        {
            School school = ((FrameworkElement)sender).DataContext as School;
            EditSchoolWindow editSchoolWindow = new EditSchoolWindow(school);
            editSchoolWindow.ShowDialog();
            if (editSchoolWindow.DialogResult == true)
            {
                SchoolDAL.UpdateSchool(editSchoolWindow.School);
                int index = schools.IndexOf(school);
                schools[index] = editSchoolWindow.School;
                LogDAL.AddLog(new Log() { LogTime = DateTime.Now.ToString(), LogUser = "管理员", LogContent = "修改学校：" + school.SchoolName });
            }
        }

        private void DeleteSchool_Click(object sender, RoutedEventArgs e)
        {
            School school = ((FrameworkElement)sender).DataContext as School;
            MessageBoxResult result = MessageBox.Show("确定删除学校：" + school.SchoolName + "？", "警告", MessageBoxButton.YesNo, MessageBoxImage.Warning);
            if (result == MessageBoxResult.Yes)
            {
                SchoolDAL.DeleteSchool(school.SchoolId);
                schools.Remove(school);
                LogDAL.AddLog(new Log() { LogTime = DateTime.Now.ToString(), LogUser = "管理员", LogContent = "删除学校：" + school.SchoolName });
            }

        }
    }

    internal class EditSchoolWindow
    {
        private School? school;

        public EditSchoolWindow(School? school)
        {
            this.school = school;
        }

        public bool DialogResult { get; internal set; }
        public School School { get; internal set; }

        internal void ShowDialog()
        {
            throw new NotImplementedException();
        }
    }

    internal class AddSchoolWindow
    {
        public bool DialogResult { get; internal set; }
        public School School { get; internal set; }

        internal void ShowDialog()
        {
            throw new NotImplementedException();
        }
    }
}
